package com.welsmann.app.start.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.welsmann.app.start.common.StartConst;

/**
 * Session常量过滤器
 * 
 * @author Welsmann
 * 
 */
public class SessionConstFilter implements Filter {
	
	private final static String SITE_ROOT = "http://127.0.0.1";

	/**
	 * Default constructor.
	 */
	public SessionConstFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// TODO Auto-generated method stub
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;

		Object objPath = req.getSession().getAttribute(
				StartConst.SESSION_SITE_PATH);
		if (objPath == null || !(objPath instanceof String)) {
			objPath = SITE_ROOT;
			req.getSession().setAttribute(StartConst.SESSION_SITE_PATH,
					objPath.toString());
		}
		Object objTheme = req.getSession().getAttribute(
				StartConst.SESSION_SITE_THEME_PATH);
		if (objTheme == null || !(objTheme instanceof String)) {
			objTheme = String.format("%s/theme", objPath.toString());
			req.getSession().setAttribute(StartConst.SESSION_SITE_THEME_PATH,
					objTheme);
		}
		Object objJs = req.getSession().getAttribute(
				StartConst.SESSION_SITE_JS_PATH);
		if (objJs == null || !(objJs instanceof String)) {
			objJs = String.format("%s/js", objPath.toString());
			req.getSession().setAttribute(StartConst.SESSION_SITE_JS_PATH,
					objJs);
		}
		chain.doFilter(req, res);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
		// TODO Auto-generated method stub
	}

}
